package com.danskin.wechat.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.dao.CrmBrandInfoDao;
import com.danskin.wechat.entity.CrmBrandInfo;
import com.danskin.wechat.service.CrmBrandInfoService;
import com.danskin.wechat.util.LogUtil;

@Service
public class CrmBrandInfoServiceImpl implements CrmBrandInfoService {

	@Autowired
	CrmBrandInfoDao crmBrandInfoDao;

	@Override
	public CrmBrandInfo selectCrmBrandInfoByMpId(int mpId) {

		CrmBrandInfo brandInfo = null;

		try {
			brandInfo = crmBrandInfoDao.selectByMpId(mpId);
		} catch (Exception e) {
			LogUtil.error("=======根据mpId查询CRM品牌异常", e);
		}
		return brandInfo;
	}

	@Override
	public CrmBrandInfo getCrmBrandInfo(String brandId) {

		return crmBrandInfoDao.selectByBrandId(brandId);
	}

	@Override
	public CrmBrandInfo selectCrmBrandInfoByAppId(String appId) {

		CrmBrandInfo brandInfo = null;

		try {
			brandInfo = crmBrandInfoDao.selectByAppId(appId);
		} catch (Exception e) {
			LogUtil.error("=======根据appId查询CRM品牌异常", e);
		}
		return brandInfo;
	}

	@Override
	public CrmBrandInfo getCrmBrandInfoByCondition(Map<String, Object> map) {
		return crmBrandInfoDao.selectBrandByCondition(map);
	}

	@Override
	public int saveCrmBrandInfo(CrmBrandInfo entity) {
		CrmBrandInfo brandInfo = crmBrandInfoDao.selectByMpId(entity.getMpId());
		if (brandInfo != null) {
			entity.setId(brandInfo.getId());
			entity.setLastModifiedTime(new Date());
		}
		CrmBrandInfo bInfo = crmBrandInfoDao.selectByBrandId(entity.getBrandId());
		// 判断是否
		if (bInfo != null && !bInfo.getId().equals(entity.getId())) {
			return -1;
		}
		if (entity.getId() == null || entity.getId() == 0) {

			return crmBrandInfoDao.insertSelective(entity);
		} else {
			return crmBrandInfoDao.updateByPrimaryKeySelective(entity);
		}
	}
}
